package com.crm.service.impl;

import com.crm.VO.LineChartVO;
import com.crm.VO.LineSeriesVO;
import com.crm.VO.PieChartVO;
import com.crm.VO.VulnerabilityMapVo;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.ContractMapper;
import com.crm.mapper.ReportMapper;
import com.crm.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ReportMapper reportMapper;



    public ReportServiceImpl() {
    }

    public List<PieChartVO> subjectStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<PieChartVO> pieChartVOList = reportMapper.subjectStatistics(beginCreateTime, endCreateTime);
        return pieChartVOList;
    }

    public LineChartVO cluesStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        ArrayList<LocalDate> dateList = new ArrayList();
        dateList.add(beginCreateTime);

        while(!beginCreateTime.equals(endCreateTime)) {
            beginCreateTime = beginCreateTime.plusDays(1L);
            dateList.add(beginCreateTime);
        }

        LineChartVO lineChartVO = new LineChartVO();
        LineSeriesVO addVo = new LineSeriesVO();
        LineSeriesVO totalVo = new LineSeriesVO();
        Integer total = 0;
        Iterator var8 = dateList.iterator();

        while(var8.hasNext()) {
            LocalDate date = (LocalDate)var8.next();
            lineChartVO.getxAxis().add(date.toString());
            Integer dateNum = this.reportMapper.getNum(date);
            addVo.getData().add(dateNum);
            total = total + dateNum;
            totalVo.getData().add(total);
        }

        addVo.setName("新增线索数");
        totalVo.setName("总线索数");
        lineChartVO.getSeries().add(addVo);
        lineChartVO.getSeries().add(totalVo);
        return lineChartVO;
    }

    public LineChartVO salesStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        ArrayList<LocalDate> dateList = new ArrayList();
        dateList.add(beginCreateTime);

        while(!beginCreateTime.equals(endCreateTime)) {
            beginCreateTime = beginCreateTime.plusDays(1L);
            dateList.add(beginCreateTime);
        }

        LineChartVO lineChartVO = new LineChartVO();
        LineSeriesVO lineSeriesVO = new LineSeriesVO();
        Iterator var6 = dateList.iterator();

        while(var6.hasNext()) {
            LocalDate date = (LocalDate)var6.next();
            Double Account = this.reportMapper.getAccount(date);
            lineSeriesVO.getData().add(Account);
            lineChartVO.getxAxis().add(date.toString());
        }

        lineSeriesVO.setName("销售金额");
        lineChartVO.getSeries().add(lineSeriesVO);
        return lineChartVO;
    }

    public LineChartVO contractStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        ArrayList<LocalDate> dateList = new ArrayList();
        dateList.add(beginCreateTime);

        while(!beginCreateTime.equals(endCreateTime)) {
            beginCreateTime = beginCreateTime.plusDays(1L);
            dateList.add(beginCreateTime);
        }

        LineChartVO lineChartVO = new LineChartVO();
        LineSeriesVO addVo = new LineSeriesVO();
        LineSeriesVO totalVo = new LineSeriesVO();
        Integer total = 0;
        Iterator var8 = dateList.iterator();

        while(var8.hasNext()) {
            LocalDate date = (LocalDate)var8.next();
            lineChartVO.getxAxis().add(date.toString());
            Integer addNumber = this.reportMapper.getAddNumber(date);
            addVo.getData().add(addNumber);
            total = total + addNumber;
            totalVo.getData().add(total);
        }

        addVo.setName("新增客户数");
        totalVo.setName("客户总数");
        lineChartVO.getSeries().add(addVo);
        lineChartVO.getSeries().add(totalVo);
        return lineChartVO;
    }

    @Override
    public VulnerabilityMapVo getVulnerabilityMap(LocalDate beginCreateTime, LocalDate endCreateTime) {

            return VulnerabilityMapVo.builder()
                    .cluesNums(clueMapper.countAllClues(beginCreateTime,endCreateTime))
                    .effectiveCluesNums(clueMapper.effectiveCluesNums(beginCreateTime,endCreateTime))
                    .businessNums(businessMapper.countByClue(beginCreateTime,endCreateTime))
                    .contractNums(contractMapper.countBybusiness(beginCreateTime,endCreateTime))
                    .build();
    }
}
